{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "cell_0",
      "metadata": {},
      "source": [
        "# ContextGem: Extracting Concepts from Specific Aspects"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "cell_1",
      "metadata": {},
      "outputs": [],
      "source": [
        "%pip install -U contextgem"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "cell_2",
      "metadata": {},
      "source": [
        "To run the extraction, please provide your LLM details in the ``DocumentLLM(...)`` constructor further below."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "cell_3",
      "metadata": {},
      "outputs": [],
      "source": [
        "# ContextGem: Extracting Concepts from Specific Aspects\n",
        "\n",
        "import os\n",
        "\n",
        "from contextgem import Aspect, Document, DocumentLLM, NumericalConcept, StringConcept\n",
        "\n",
        "\n",
        "# Sample text content\n",
        "text_content = \"\"\"\n",
        "DataFlow Systems is an innovative fintech startup that was established in 2020 in Austin, Texas.\n",
        "The company has rapidly grown to 150 employees and operates in 8 major cities across North America.\n",
        "DataFlow's core platform, FinanceStream, is used by more than 5,000 small businesses for automated accounting.\n",
        "In their latest financial report, DataFlow Systems announced $12 million in annual revenue for 2024.\n",
        "This represents an impressive 40% increase compared to their 2023 performance.\n",
        "The company has secured $25 million in Series B funding and plans to expand internationally next year.\n",
        "\"\"\"\n",
        "\n",
        "# Create a Document object from text\n",
        "doc = Document(raw_text=text_content)\n",
        "\n",
        "# Define an aspect to extract from the document\n",
        "financial_aspect = Aspect(\n",
        "    name=\"Financial Performance\",\n",
        "    description=\"Revenue, growth metrics, and financial indicators\",\n",
        ")\n",
        "\n",
        "# Add concepts to the aspect\n",
        "financial_aspect.concepts = [\n",
        "    StringConcept(\n",
        "        name=\"Annual Revenue\",\n",
        "        description=\"Total revenue reported for the year\",\n",
        "    ),\n",
        "    NumericalConcept(\n",
        "        name=\"Growth Rate\",\n",
        "        description=\"Percentage growth rate compared to previous year\",\n",
        "        numeric_type=\"float\",\n",
        "    ),\n",
        "    NumericalConcept(\n",
        "        name=\"Revenue Year\",\n",
        "        description=\"The year for which revenue is reported\",\n",
        "    ),\n",
        "]\n",
        "\n",
        "# Attach the aspect to the document\n",
        "doc.aspects = [financial_aspect]\n",
        "\n",
        "# Configure DocumentLLM with your API parameters\n",
        "llm = DocumentLLM(\n",
        "    model=\"azure/gpt-4.1\",\n",
        "    api_key=os.getenv(\"CONTEXTGEM_AZURE_OPENAI_API_KEY\"),\n",
        "    api_version=os.getenv(\"CONTEXTGEM_AZURE_OPENAI_API_VERSION\"),\n",
        "    api_base=os.getenv(\"CONTEXTGEM_AZURE_OPENAI_API_BASE\"),\n",
        ")\n",
        "\n",
        "# First, extract the aspect from the document (required before concept extraction)\n",
        "extracted_aspects = llm.extract_aspects_from_document(doc)\n",
        "financial_aspect = extracted_aspects[0]\n",
        "\n",
        "# Extract concepts from the specific aspect\n",
        "extracted_concepts = llm.extract_concepts_from_aspect(financial_aspect, doc)\n",
        "\n",
        "# Access extracted concepts for the aspect\n",
        "print(f\"Aspect: {financial_aspect.name}\")\n",
        "print(f\"Extracted items: {[item.value for item in financial_aspect.extracted_items]}\")\n",
        "print(\"\\nConcepts extracted from this aspect:\")\n",
        "for concept in extracted_concepts:\n",
        "    print(f\"  {concept.name}: {[item.value for item in concept.extracted_items]}\")\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.10.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}